package com.example.mydataapplication;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Internal extends Fragment {
    public Internal(){
        super(R.layout.internal);
    }
    private final String FILE_NAME="Internal.txt";
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view,savedInstanceState);
        getView().findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(
                            getActivity().openFileOutput(FILE_NAME, Context.MODE_PRIVATE)));
                    bw.write("HelloWorld");
                    bw.close();
                    Toast.makeText(getActivity(),"内部写入成功",Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        getView().findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    BufferedReader br=new BufferedReader(new InputStreamReader(getActivity().openFileInput(FILE_NAME)));
                    Toast.makeText(getActivity(),br.readLine(),Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
